//
//  GuideViewModel.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import Foundation
import UIKit
import SafariServices
import FirebaseAnalytics
import SwiftyJSON

extension String {
    var urlConvert: URL? {
        get {
            return URL(string: self) ?? nil
        }
    }
}

extension UIApplication {
    static func openWebUrl(urlStr: String) {
        let viewController = UIViewController.topViewController()
        if let webURL = urlStr.urlConvert {
            let safari = SFSafariViewController(url: webURL)
            viewController?.presentFullStyle(safari)
        }
    }
}

class GuideViewModel:ObservableObject {
    
    init() {
        Analytics.logEvent("start_page_show", parameters: nil)
    }
    
    
    func user(){
            UIApplication.openWebUrl(urlStr:"https://aichattings.com/terms-of-use/")
    }
    
    func privacy(){
            UIApplication.openWebUrl(urlStr:"https://aichattings.com/privacy-policy/")
    }
    
    func register(){
        RequestManager.shared.postWithJson("https://backend.aichattings.com/api/v2/user/register", parameters: ["uuid":(UUID().uuidString),
                                                                                                               "endpoint_type":"ios",
                                                                                                               "subscribe_type":"0"]) { result in
            
            switch result {
            case .success(let data):
                let j = JSON(data)
                if let ep_user_id = j["data"]["id"].string {
                    UserDefaults.standard.setValue(ep_user_id, forKey: "ep_user_id")
                    UserDefaults.standard.synchronize()
                }
            case .failure(_): break
                
            }
            
            
        }
    }
    
}

extension GuideViewModel {
    func logContinue(){
        Analytics.logEvent("start_page_continue", parameters: nil)
    }
    
    func logshow1(){
        Analytics.logEvent("start_intro1_show", parameters: nil)
    }
    func logshow0(){
        Analytics.logEvent("start_intro0_show", parameters: nil)
    }
    
    func logshow2(){
        Analytics.logEvent("start_intro2_show", parameters: nil)
    }
    
    func log1Continue(){
        Analytics.logEvent("start_intro1_continue", parameters: nil)
    }
    
    func log2Continue(){
        Analytics.logEvent("start_intro2_continue", parameters: nil)
    }    
    func log0Continue(){
        Analytics.logEvent("start_intro0_continue", parameters: nil)
    }
    
    
}
